<template>
  <view class="sendRed">
    <topTitle title="发红包" />

    <view class="ipts">
      <view v-if="isGroupChat">
        <view>红包个数</view>
        <view>
          <up-input
            placeholder="請輸入红包个数"
            border="none"
            v-model="number"
            type="number"
            clearable
          >
            <template #suffix> 个 </template>
          </up-input>
        </view>
      </view>
      <view>
        <view>金额</view>
        <view>
          <up-input
            placeholder="請輸入红包总金额 $0.00"
            border="none"
            v-model="money"
            type="number"
            clearable
          />
        </view>
      </view>

      <view>
        <view>备注</view>
        <view>
          <up-input
            :placeholder="defaultRemarks"
            border="none"
            v-model="remarks"
            type="text"
            clearable
          />
        </view>
      </view>
    </view>
    <view class="btn">
      <up-button
        :disabled="!money"
        text="立即发送"
        @tap="PaymentPwSetFn"
        type="primary"
      ></up-button>
    </view>
    <psw ref="pswRef" @pswFinish="pswFinish" title="发红包" :money="money" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { onShow, onLoad } from "@dcloudio/uni-app";
import { PersonalSend, GroupSend } from "@/api/ImApi";
import { setChatList } from "@/static/ts/common";
const instance = getCurrentInstance()?.appContext.config.globalProperties;

let myUserInfo = ref();
let optionsValue = ref<any>();
let chatId = ref<any>();
let baseDataType = ref<any>();
const isGroupChat = ref<boolean>(false);

onLoad((e: any) => {
  optionsValue.value = e;
  chatId.value = e.chatId;
  if (e.isGroupChat == "false") {
    isGroupChat.value = false;
  } else {

    isGroupChat.value = true;
  }
  baseDataType.value = isGroupChat.value ? "groupChat" : "singleChat";
});
onShow(() => {
  myUserInfo.value = uni.getStorageSync("userInfo");
});

let money = ref<number>();
let number = ref<number>();
let remarks = ref<string>();
let defaultRemarks = ref<string>("红包飞至，幸运开启");
let pswRef = ref<any>(null);
// 设置支付密码
const PaymentPwSetFn = async () => {
  if (isGroupChat.value && !number.value) {
    uni.showToast({
      title: "请输入红包数量",
      icon: "none",
    });
    return;
  }
  if (!myUserInfo.value.payment_password_set) {
    uni.showModal({
      title: "温馨提示",
      content: "请先设置支付密码",
      confirmText: "确认",
      cancelText: "取消",
      success: async (r) => {
        if (r.confirm) {
          instance?.goTo("/pages/pay/paymentPassword");
        }
      },
    });

    return;
  }
  pswRef.value.openPsw();
};
const pswFinish = async (e: string) => {
  let obj: any = {
    message_type: 6,
    message_content: {
      money: Number(money.value),
      number: Number(number.value) || 1,
      remarks: remarks.value || defaultRemarks.value,
      payment_password: e,
    },
  };
  let res: any = {};
  let Type = 1;
  uni.showLoading()
  if (optionsValue.value.isGroupChat == "false") {
    obj["user_id"] = Number(chatId.value);
	obj["id"] = Number(chatId.value);
    Type = 1;
    res = await PersonalSend(obj);
  } else {
    obj["group_id"] = chatId.value;
	obj["id"] =chatId.value;
    Type = 2;
    res = await GroupSend(obj);
  }
  obj["chatId"] = chatId.value;
 
  instance?.$db.saveData(baseDataType.value, [
    { ...obj, ...res, id: chatId.value + "_" + res.MessageId },
  ]);
  await setChatList({ ...res, ...obj, Type }, chatId.value );
  uni.setStorageSync("submitSendRed", 1);
  uni.hideLoading()
  instance?.goBack(1);
};
</script>

<style scoped lang="scss">
@import "../../static/scss/red.scss";
</style>
